package com.wwwarehouse.usercenter.activity;

import android.app.Activity;
import android.app.Dialog;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.wei.android.lib.fingerprintidentify.FingerprintIdentify;
import com.wei.android.lib.fingerprintidentify.base.BaseFingerprint;
import com.wwwarehouse.common.activity.base.BaseApplication;
import com.wwwarehouse.common.common.Common;
import com.wwwarehouse.common.constant.Constant;
import com.wwwarehouse.common.tools.BitmapUtil;
import com.wwwarehouse.common.tools.DialogTools;
import com.wwwarehouse.common.tools.LogUtils;
import com.wwwarehouse.usercenter.R;
import com.wwwarehouse.usercenter.constant.UserCenterConstant;

@Route(path = Constant.PATH_FINGERLOGIN_ACTIVITY)
public class FingerLockActivity extends Activity implements View.OnClickListener {

    private ImageView mIvFinger;
    private TextView mTvFingerLock;
    private TextView mTvElseWay;
    private Bitmap mBitmap;
    private FingerprintIdentify mFingerprintIdentify;
    private  int MAX_AVAILABLE_TIMES = 5;  //识别10次就自动结束识别
    private DialogRunnable mDialogRunnable;
    private Handler mHandler;
    private TextView mTvTimes;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_finger_lock);
        initView();
    }

    private void initView() {

        mIvFinger = (ImageView) findViewById(R.id.iv_finger);//指纹的图片
        mIvFinger.setOnClickListener(this);
        mTvFingerLock = (TextView) findViewById(R.id.tv_finger_lock);//点击的文字
        mTvElseWay = (TextView) findViewById(R.id.tv_else_way);//其他方式的文字
        mTvElseWay.setOnClickListener(this);
        mBitmap = BitmapUtil.drawableToBitmap(getResources().getDrawable(R.drawable.finger_unlock_confirm));
        mFingerprintIdentify = new FingerprintIdentify(BaseApplication.getApplicationInstance(), new BaseFingerprint.FingerprintIdentifyExceptionListener() {
            @Override
            public void onCatchException(Throwable exception) {
                LogUtils.showLog(exception.getLocalizedMessage());
            }
        });
        mHandler = new Handler();
        mDialogRunnable = new DialogRunnable();
        mHandler.postDelayed(mDialogRunnable, 200);

    }

    private Dialog mDialogFinger;
    class DialogRunnable implements Runnable{

        @Override
        public void run() {
            DialogTools.getInstance().showCustomFinger(FingerLockActivity.this, false, mBitmap, FingerLockActivity.this.getResources().getString(R.string.finger_home),
                    FingerLockActivity.this.getResources().getString(R.string.quit), new DialogTools.ConfirmListener() {
                @Override
                public void setConfirmListener(Dialog dialog, View view, String s) {
                    mDialogFinger = dialog;
                    mTvTimes = (TextView) (view).findViewById(R.id.tv_content_finger);
                }
            }, new DialogTools.DismissListener() {
                @Override
                public void setDismissListener(Dialog dialog, View view) {
                    dialog.dismiss();
                    mFingerprintIdentify.cancelIdentify();
                }
            });
            startFinger();

        }
    }
    public void startFinger(){
        //开启指纹解锁
        mFingerprintIdentify.startIdentify(MAX_AVAILABLE_TIMES, new BaseFingerprint.FingerprintIdentifyListener() {
            @Override
            public void onSucceed() {
                //指纹识别成功
                if (mDialogFinger != null) {
                    mDialogFinger.dismiss();
                }
                finish();

            }

            @Override
            public void onNotMatch(int availableTimes) {
                LogUtils.showLog("availableTimes: " + availableTimes);
                mTvTimes.setText(getResources().getString(R.string.wrong)+availableTimes+getResources().getString(R.string.times));
            }

            @Override
            public void onFailed(boolean isDeviceLocked) {
                LogUtils.showLog("onFailed          "+isDeviceLocked);
                if (mDialogFinger != null) {
                    mDialogFinger.dismiss();
                }
                Toast.makeText(FingerLockActivity.this, (getResources().getString(R.string.finger_waite)), Toast.LENGTH_SHORT).show();

            }

            @Override
            public void onStartFailedByDeviceLocked() {
                LogUtils.showLog("onStartFailedByDeviceLocked");
                mFingerprintIdentify.cancelIdentify();
                Toast.makeText(FingerLockActivity.this, (getResources().getString(R.string.finger_waite)), Toast.LENGTH_SHORT).show();

                mHandler.removeCallbacks(mDialogRunnable);
            }
        });

    }
    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.iv_finger) {
            mIvFinger.setImageResource(R.drawable.finger_unlock_click);
            mHandler.postDelayed(mDialogRunnable, 200);
        } else if (id==R.id.tv_else_way) {
//            startActivity(UserCenterConstant.PATH_LOGIN_ACTIVITY, null, false);
            if (Common.getInstance().isNotFastClick()) {
                ARouter.getInstance()
                        .build(UserCenterConstant.PATH_LOGIN_ACTIVITY)
                        .with(null)
                        .withTransition(R.anim.comm_slide_in_from_right, R.anim.comm_scale_out)
                        .navigation(this);

            }
        }

    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        return true;
    }
}
